package com.abraham.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;

/**
 * Copyright (C), 2013-2020, 深圳市浩瀚卓越科技有限公司
 * Author: Abraham.ai@hohem-tech.com
 * Date: 2020/5/27 14:06
 * Description:
 * History:
 */
public class PermissionDialogUtil {
    private static PermissionDialogUtil instance=new PermissionDialogUtil();



    public static PermissionDialogUtil getInstance() {

        return instance;
    }

    /**
     * 权限提示
     */
    public void showPermissionDialog(Context mContext , final  PermissionDialogUtilListener  listener)
    {
        String title=mContext.getResources().getString(R.string.permission_title);
        String content=mContext.getResources().getString(R.string.permission_content);
        new MaterialDialog.Builder(mContext)
                .title(title)
                .content(content)
                .positiveText(mContext.getResources().getString(R.string.txt_continue))
                .negativeText(mContext.getResources().getString(R.string.cancel))
                .positiveColor(mContext.getResources().getColor(R.color.colorPrimary))
                .negativeColor(mContext.getResources().getColor(R.color.colorPrimary))
                .widgetColor(mContext.getResources().getColor(R.color.colorPrimary))//不再提醒的checkbox 颜色
                .onAny(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which)
                    {
                        switch (which)
                        {
                            case NEGATIVE:
                            case POSITIVE:
                                listener.requestPermission();
                                break;
                        }
                    }
           }).show();
    }


    /**
     * 权限提示
     */
    public void showPermissionFailDialog(final  Context mContext)
    {
        String title=mContext.getResources().getString(R.string.permission_fail_title);
        String content=mContext.getResources().getString(R.string.permission_fail_content);
        new MaterialDialog.Builder(mContext)
                .title(title)
                .content(content)
                .positiveText(mContext.getResources().getString(R.string.btn_go_settings))
                .positiveColor(mContext.getResources().getColor(R.color.colorPrimary))
                .widgetColor(mContext.getResources().getColor(R.color.colorPrimary))//不再提醒的checkbox 颜色
                .onAny(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which)
                    {
                        switch (which)
                        {
                            case NEGATIVE:
                            case POSITIVE:
                                Intent intent = new Intent();
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                if(Build.VERSION.SDK_INT >= 9){
                                    intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                                    intent.setData(Uri.fromParts("package", mContext.getPackageName(), null));
                                } else if(Build.VERSION.SDK_INT <= 8){
                                    intent.setAction(Intent.ACTION_VIEW);
                                    intent.setClassName("com.android.settings","com.android.settings.InstalledAppDetails");
                                    intent.putExtra("com.android.settings.ApplicationPkgName",mContext . getPackageName());
                                }
                                mContext. startActivity(intent);
                                break;
                        }
                    }
                }).show();
    }


    public boolean isCheckPermission(Context context ,String[] permissions)
    {
        PackageManager packageManager=context.getPackageManager();
        boolean isPermission=false;
        for (int i = 0; i < permissions.length; i++) {

            isPermission = (PackageManager.PERMISSION_GRANTED == packageManager.checkPermission(permissions[i], context.getPackageName()));

            if(!isPermission)
            {
                Log.i("PermissionLog ", " 没有权限" +permissions[i]);
                return  false;
            }
        }
        return isPermission;


        //ActivityCompat.shouldShowRequestPermissionRationale(SplashActivity.this, permission)
    }

    /**
     * 是否有拒绝的权限
     * @param context
     * @param permissions
     * @return
     */
    public boolean isCheckRefusePermission(Activity activity ,Context context , String[] permissions)
    {
        PackageManager packageManager=context.getPackageManager();
        boolean isPermission=false;
        for (int i = 0; i < permissions.length; i++) {

            isPermission = ActivityCompat.shouldShowRequestPermissionRationale(activity, permissions[i]);

            if(isPermission)
            {
                Log.i("PermissionLog",  " Permission :" +permissions[i]);
                return  true;
            }
        }
        return isPermission;
    }

    interface PermissionDialogUtilListener
    {
        void requestPermission();
    }
}
